{
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
}

unit TestDocumentEditor;

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
  ActnList, ExtCtrls, Shape, VisualUtils, TestEditor, superobject,
  ProfilesEditor, base64, MiscUtils, About;

type
  TTestDocumentEditorFrame = class(TShapeFrame)
    actExportToExe: TAction;
    actCreate: TAction;
    actClose: TAction;
    actExit: TAction;
    actShowAbout: TAction;
    actOpen: TAction;
    actSave: TAction;
    actShowProfiles: TAction;
    alMain: TActionList;
    bvlTop: TBevel;
    ilMain: TImageList;
    pnlExtra: TPanel;
    pnlRight: TPanel;
    pnlTop: TPanel;
    pnlMain: TPanel;
    tbCreate: TToolButton;
    tbExportToExe: TToolButton;
    tbOpen: TToolButton;
    tbrMain: TToolBar;
    tbrRight: TToolBar;
    tbSave: TToolButton;
    tbSeparator1: TToolButton;
    tbShowProfiles: TToolButton;
    tbClose: TToolButton;
    tbExit: TToolButton;
    tbSeparator2: TToolButton;
    tbShowAbout: TToolButton;
    procedure actCloseExecute(Sender: TObject);
    procedure actCreateExecute(Sender: TObject);
    procedure actExitExecute(Sender: TObject);
    procedure actExportToExeExecute(Sender: TObject);
    procedure actOpenExecute(Sender: TObject);
    procedure actSaveExecute(Sender: TObject);
    procedure actShowAboutExecute(Sender: TObject);
    procedure actShowProfilesExecute(Sender: TObject);
  public type
    TTestDocumentEvent = procedure(Sender: TTestDocumentEditorFrame) of object;
  private
    FRendered: Boolean;
    FTestEditor: TTestEditorFrame;
    FProfilesEditor: TProfilesEditorFrame;
    FSelectMe: Boolean;
    FOnSave: TTestDocumentEvent;
    FOnOpen: TTestDocumentEvent;
    FOnCreate: TTestDocumentEvent;
    FOnClose: TTestDocumentEvent;
    FOnExit: TTestDocumentEvent;
    FOnExportToExe: TTestDocumentEvent;
    function TestPageSelected: Boolean;
    { private declarations }
  public
    procedure AfterConstruction; override;
    destructor Destroy; override;
    procedure Render;
    function GetTestData: String;
    procedure MarkAsUnmodified;
    function ExtraPanel: TPanel;
    function Modified: Boolean;
    function ExportTest: String;

    property OnClose: TTestDocumentEvent read FOnClose write FOnClose;
    property OnCreate: TTestDocumentEvent read FOnCreate write FOnCreate;
    property OnExit: TTestDocumentEvent read FOnExit write FOnExit;
    property OnExportToExe: TTestDocumentEvent read FOnExportToExe write FOnExportToExe;
    property OnOpen: TTestDocumentEvent read FOnOpen write FOnOpen;
    property OnSave: TTestDocumentEvent read FOnSave write FOnSave;
    { public declarations }
  end;

implementation

{$R *.lfm}
{$R *.rc}

{ TTestDocumentEditorFrame }

procedure TTestDocumentEditorFrame.actShowProfilesExecute(Sender: TObject);
var
  NewPage: String;
  Input: ISuperObject;
begin
  if TestPageSelected then
    NewPage := 'PROFILES'
  else
    NewPage := 'TEST';
  Input := SO;
  Input.S['page'] := NewPage;
  Dispatcher.PerformAction(Id, 'selectPage', Input);

  FSelectMe := TRUE;
end;

procedure TTestDocumentEditorFrame.actExportToExeExecute(Sender: TObject);
begin
  if Assigned(FOnExportToExe) then
    FOnExportToExe(Self);
end;

procedure TTestDocumentEditorFrame.actCreateExecute(Sender: TObject);
begin
  if Assigned(FOnCreate) then
    FOnCreate(Self);
end;

procedure TTestDocumentEditorFrame.actExitExecute(Sender: TObject);
begin
  if Assigned(FOnExit) then
    FOnExit(Self);
end;

procedure TTestDocumentEditorFrame.actCloseExecute(Sender: TObject);
begin
  if Assigned(FOnClose) then
    FOnClose(Self);
end;

procedure TTestDocumentEditorFrame.actOpenExecute(Sender: TObject);
begin
  if Assigned(FOnOpen) then
    FOnOpen(Self);
end;

procedure TTestDocumentEditorFrame.actSaveExecute(Sender: TObject);
begin
  if Assigned(FOnSave) then
    FOnSave(Self);
end;

procedure TTestDocumentEditorFrame.actShowAboutExecute(Sender: TObject);
begin
  TAboutForm.OpenModal;
end;

function TTestDocumentEditorFrame.TestPageSelected: Boolean;
begin
  Result := Shape.S['page'] = 'TEST';
end;

procedure TTestDocumentEditorFrame.AfterConstruction;
begin
  inherited;
  alMain.OnExecute := Scene.ActionListExecuteFilter;
  LoadActionImages(alMain);
end;

destructor TTestDocumentEditorFrame.Destroy;
begin
  FreeAndNil(FTestEditor);
  FreeAndNil(FProfilesEditor);
  inherited;
end;

procedure TTestDocumentEditorFrame.Render;
begin
  if UpdateShape or not FRendered then
  begin
    actSave.Enabled := Modified;

    if TestPageSelected then
    begin
      if FTestEditor = nil then
        FTestEditor := TTestEditorFrame.Create(Shape.S['testEditor'], Dispatcher, Self);
      if FProfilesEditor <> nil then
        FProfilesEditor.Visible := FALSE;
      ShowAlignedControlReliably(FTestEditor);
    end
    else
    begin
      if FProfilesEditor = nil then
        FProfilesEditor := TProfilesEditorFrame.Create(Shape.S['profilesEditor'], Dispatcher, Self);
      if FTestEditor <> nil then
        FTestEditor.Visible := FALSE;
      ShowAlignedControlReliably(FProfilesEditor);
    end;
    actShowProfiles.Checked := not TestPageSelected;
  end;

  if TestPageSelected then
    FTestEditor.Render
  else
    FProfilesEditor.Render;

  if FSelectMe then
  begin
    SelectFirst;
    FSelectMe := FALSE;
  end;

  FRendered := TRUE;
end;

function TTestDocumentEditorFrame.GetTestData: String;
var
  Res: ISuperObject;
begin
  Res := Dispatcher.PerformAction(Id, 'get');
  Result := DecodeStringBase64(Res.S['test']);
end;

procedure TTestDocumentEditorFrame.MarkAsUnmodified;
begin
  Dispatcher.PerformAction(Id, 'markAsUnmodified');
end;

function TTestDocumentEditorFrame.ExtraPanel: TPanel;
begin
  Result := pnlExtra;
end;

function TTestDocumentEditorFrame.Modified: Boolean;
begin
  Result := Shape.B['modified'];
end;

function TTestDocumentEditorFrame.ExportTest: String;
var
  Res: ISuperObject;
begin
  Res := Dispatcher.PerformAction(Id, 'export');
  Result := DecodeStringBase64(Res.S['test']);
end;

end.

